package com.dofast.bt.mvp.presenter.basket;

import com.dofast.bt.adapter.SelectCallBack;
import com.dofast.bt.adapter.SubmitOrderAdapter;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.AddressBean;
import com.dofast.bt.bean.ShoppingCartBean;
import com.dofast.bt.mvp.view.activity.basket.SubmitOderView;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * username:Created by hsj on 2016/12/18 0018.
 * class：
 * 类描述：确认付款
 */

public class SubmitOrderPresenter extends BaseMvpPresenter<SubmitOderView> implements ISubmitOrderPresenter {
    private List<ShoppingCartBean> list = null;
    private AddressBean address = null;
    private SubmitOrderAdapter adapter = null;

    @Override
    public void initData() {
        checkViewAttach();
        list = new ArrayList<ShoppingCartBean>();
        address = new AddressBean();
        list = mvpView.getGoods();
        totleNum();
        adapter = new SubmitOrderAdapter(list, new SelectCallBack() {
            @Override
            public void select(int position, boolean flags) {
                int num = list.get(position).getNum();
                if (flags) {
                    num++;
                    list.get(position).setNum(num);
                } else {
                    if (num > 1) {
                        num--;
                    } else {
                        num = 1;
                    }
                    list.get(position).setNum(num);
                }
                totleNum();
                adapter.notifyDataSetChanged();
            }
        });
        mvpView.initAdapter(adapter);
    }

    private void totleNum(){
        try {
        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            int num = list.get(i).getNum();
            double price = list.get(i).getGoods().getPrice();
            sum += num*price;
        }
        mvpView.setAllMoney(sum + "");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void pay() {

        mvpView.gotoPay(mvpView.getAddress(), list);


    }

    @Override
    public void showDialog() {

    }

    @Override
    public void changeAddress() {
        mvpView.changeAddress();
    }

    public void modifyAddress(AddressBean addressBean) {
        mvpView.setAddress(addressBean);
    }
}
